﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using LGSVR.Common;
using System.Data;
using System.Transactions;

namespace LGSVR.BLL.Program
{
    public class ActMenu
    {
        /// <summary>
        /// 更新或添加影响行
        /// </summary>
        protected int Result
        {
            get;
            set;
        }
       
        // /// <summary>
        ///// 修改数据状态
        ///// </summary>
        ///// <param name="tbName"></param>
        ///// <param name="state"></param>
        ///// <param name="lstId"></param>
        //public virtual bool UpdateState(string tbName, string lstId)// string stractID)
        //{
        //    StringBuilder sbEditSQL;
        //    List<SqlParameter> lstParam = new List<SqlParameter>();
        //    sbEditSQL = new StringBuilder();

        //    sbEditSQL.Insert(0, "update " + tbName + " set state=@state");
        //    sbEditSQL.Append(" where ID=@ID AND actID=@actID");
        //    try
        //    {
        //        using (TransactionScope tran = new TransactionScope())
        //        {
        //            foreach (var id in lstId.Split(','))
        //            {
        //                lstParam.Add(new SqlParameter("@state", id.Split('$')[1]));
        //                //lstParam.Add(new SqlParameter("@actID", stractID));
        //                lstParam.Add(new SqlParameter("@ID", id.Split('$')[0]));
        //                Result = SQLHelper.ExecuteNonQuery(sbEditSQL.ToString(), CommandType.Text, lstParam.ToArray());
        //                lstParam.Clear();
        //            }
        //            OpLogBLL.SaveLog("胡志瑶", LogLoginType.W, "设置" + tbName + "信息,IDs:" + lstId, LogOpType.修改日志, DateTime.Now, "");
        //            tran.Complete();
        //            return true;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        OpLogBLL.SaveLog("胡志瑶", LogLoginType.W, "设置" + tbName + "信息,IDs:" + lstId, LogOpType.修改日志, DateTime.Now, ex.ToString());
        //        return false;
        //    }
          
        //}
        ///// <summary>
        ///// 修改数据状态
        ///// </summary>
        ///// <param name="tbName"></param>
        ///// <param name="state"></param>
        ///// <param name="lstId"></param>
        //public virtual bool UpdateState(string tbName, object state, string ActID)
        //{
        //    StringBuilder sbEditSQL;
        //    List<SqlParameter> lstParam = new List<SqlParameter>();
        //    sbEditSQL = new StringBuilder();

        //    sbEditSQL.Insert(0, "update " + tbName + " set state=@state");
        //    sbEditSQL.Append(" where ActID=@ActID");
          
        //    try
        //    {
        //        using (TransactionScope tran = new TransactionScope())
        //        {
        //            lstParam.Add(new SqlParameter("@state", state));
        //            lstParam.Add(new SqlParameter("@ActID", ActID));
        //            Result = SQLHelper.ExecuteNonQuery(sbEditSQL.ToString(), CommandType.Text, lstParam.ToArray());                   
        //            OpLogBLL.SaveLog("胡志瑶", LogLoginType.W, "停用" + tbName + "信息,IDs:" + "全部", LogOpType.停用日志, DateTime.Now, "");
        //            tran.Complete();
        //            return true;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        OpLogBLL.SaveLog("胡志瑶", LogLoginType.W, "停用" + tbName + "信息,IDs:" + "全部", LogOpType.停用日志, DateTime.Now, ex.ToString());
        //        return false;
        //    }

        //}

      



        /// <summary>
        /// 修改数据状态
        /// </summary>
        /// <param name="tbName"></param>
        /// <param name="state"></param>
        /// <param name="lstId"></param>
        public virtual bool UpdateState(string tbName, object state, string strKey, string lstId,string userid)
        {
            StringBuilder sbEditSQL;
            List<SqlParameter> lstParam = new List<SqlParameter>();
            sbEditSQL = new StringBuilder();

            sbEditSQL.Insert(0, "update " + tbName + " set state=@state");
            sbEditSQL.Append(" where " + strKey +"=@ID");
            try
            {
                using (TransactionScope tran = new TransactionScope())
                {
                    foreach (var id in lstId.Split(','))
                    {
                        lstParam.Add(new SqlParameter("@state", state));
                        lstParam.Add(new SqlParameter("@ID", id));
                        Result = SQLHelper.ExecuteNonQuery(sbEditSQL.ToString(), CommandType.Text, lstParam.ToArray());
                        lstParam.Clear();
                    }
                    OpLogBLL.SaveLog(userid, LogLoginType.W, "停用" + tbName + "信息,IDs:" + lstId, LogOpType.停用日志, DateTime.Now, "");
                    tran.Complete();
                    return true;
                }
            }
            catch (Exception ex)
            {
                OpLogBLL.SaveLog(userid, LogLoginType.W, "停用" + tbName + "信息,IDs:" + lstId, LogOpType.停用日志, DateTime.Now, ex.ToString());
                return false;
            }

        }
    }
}
